/*
 * Spring 2012 TCSS360 Deliverable 3
 * Matthew Selfors
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import model.Bidder;
import model.Item;

@SuppressWarnings("serial")
public class BidListGUI extends JPanel {

	private Bidder bidder;

	private JPanel item_list;

	private JFrame frame;

	private JTextArea item_num;

	private JTextArea bid_amount;

	private Font font;

	public BidListGUI(final Bidder the_bidder) {
		super();
		frame = new JFrame();
		bidder = the_bidder;

		frame.setPreferredSize(new Dimension(300, 500));
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		item_list = new JPanel(new BorderLayout());
		JScrollPane scroll = new JScrollPane(item_list);
		frame.add(this);
		setLayout(new BorderLayout());
		add(scroll, BorderLayout.CENTER);
		font = new Font("auction", 0, 18);
		item_list.setBackground(Color.WHITE);
		scroll.getVerticalScrollBar().setUnitIncrement(20);

		setUpTitle();

		setUpBidList();

		frame.pack();
		frame.setVisible(true);
	}

	private void setUpTitle() {
		JPanel title = new JPanel(new BorderLayout());
		item_num = new JTextArea("Item number:");
		bid_amount = new JTextArea("Bid amount:");
		item_num.setFont(font);
		bid_amount.setFont(font);
		item_num.setEditable(false);
		bid_amount.setEditable(false);
		title.add(item_num, BorderLayout.WEST);
		title.add(bid_amount, BorderLayout.EAST);
		title.setBackground(Color.WHITE);
		item_num.setBackground(Color.WHITE);
		bid_amount.setBackground(Color.WHITE);
		add(title, BorderLayout.NORTH);
	}

	private void setUpBidList() {
		JTextArea item_text;
		JTextArea bid_text;
		JLabel total_text;
		String item_temp = "";
		String bid_temp = "";
		BigDecimal total = new BigDecimal(0);

		Iterator<Entry<Item, BigDecimal>> it =
				bidder.getBids().entrySet().iterator();

		while (it.hasNext()) {
			Entry<Item, BigDecimal> eSet = it.next();
			Item i = eSet.getKey();
			System.out.println(i);
			BigDecimal bd = eSet.getValue();
			item_temp += (i.getItemName()) + "\n";
			bid_temp +=
					(NumberFormat.getCurrencyInstance()
							.format(bd)) + "\n";
			total = total.add(bd);
		}

		total_text =
				new JLabel("Total: "
						+ NumberFormat
								.getCurrencyInstance()
								.format(total));
		item_text = new JTextArea(item_temp);
		bid_text = new JTextArea(bid_temp);

		item_text.setEditable(false);
		bid_text.setEditable(false);
		item_text.setFont(font);
		bid_text.setFont(font);
		total_text.setFont(font);

		JPanel item_panel = new JPanel();
		item_panel.setLayout(new BoxLayout(item_panel,
				BoxLayout.PAGE_AXIS));
		item_panel.add(item_text);
		JPanel bids_panel = new JPanel();
		bids_panel.setLayout(new BoxLayout(bids_panel,
				BoxLayout.PAGE_AXIS));
		bids_panel.add(bid_text);
		total_text
				.setHorizontalAlignment(SwingConstants.RIGHT);

		item_list.add(item_panel, BorderLayout.WEST);
		item_list.add(bids_panel, BorderLayout.EAST);
		item_list.add(total_text, BorderLayout.SOUTH);
	}
}
